/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package deatax.state;

import deatax.GameData;
import deatax.Main;
import deatax.display.gui.MainMenu;
import deatax.network.Client;
import deatax.network.Messages;
import deatax.network.NetworkListener;
import java.awt.BorderLayout;
import java.awt.Color;

/**
 *
 * @author pson
 */
public class MainMenuState extends GameState implements NetworkListener {

    MainMenu mm;

    public MainMenuState(GameData data) {
        super("MainMenu", data);
    }

    @Override
    public void begin() {
        mm = new MainMenu();
        data.getFrame().getContentPane().add(mm, BorderLayout.CENTER);
        Client.get().addListener(this);
    }

    @Override
    public void finish() {
        data.getFrame().remove(mm);
        Main.showLoading();
        Client.getData().beginGame();
        Client.getData().changeGameState("Placement");
        Main.stopLoading();
    }

    @Override
    public void processMessage(int type, String[] arg) {
        switch (type) {
            case Messages.MAP_DEFINE: {
                int seed = Integer.parseInt(arg[0]);
                String landmass = arg[1];
                int size = Integer.parseInt(arg[2]);
                String mode = arg[3];
                Client.getData().setMapParams(seed, landmass, size);
                Client.getData().setGameMode(mode);
                finish();
            }
            break;
            case Messages.PLAYER_SET: {
                for (int i = 0; i < arg.length; i += 2) {
                    String pname = arg[i];
                    int color = Integer.parseInt(arg[i + 1]);
                    Client.getData().addPlayer(pname, new Color(color));
                }
                Client.getData().setSelf(mm.getSelfPlayer());
            }
            break;
            default:
                break;
        }
    }
}
